function PlotNB(ax,sch)
% PlotNB(ax,sch)
% 
% ax = handle to axes object
% sch = schedule structure
% 
% DJS (c) 2010

if isempty(ax), ax = gca; end

h = 2;      % hits
% m = 1;      % misses
f = 3;      % false alarm

rv = sch.response_vals;

% find trial types
t = strfind(sch.readparams,'trial_type');
for i = 1:length(t)
    if ~isempty(t{i}), xtt = i+1; break; end
end
catch_trials = rv(:,xtt) == 1;

% find response codes
t = strfind(sch.readparams,'response_code');
for i = 1:length(t)
    if ~isempty(t{i}), xrc = i+1; break; end
end

% find HP frequencies of noise stimuli
t = strfind(sch.readparams,'NoiseHP');
for i = 1:length(t)
    if ~isempty(t{i}), xhp = i+1; break; end
end
uhp = unique(rv(~catch_trials,xhp));

% find DB levels of noise stimuli
t = strfind(sch.readparams,'NoiseDB');
for i = 1:length(t)
    if ~isempty(t{i}), xdb = i+1; break;    end
end
udb = unique(rv(~catch_trials,xdb));

% compute hit rate for each Frequency & DB level
hr = zeros(length(udb),length(uhp));
for i = 1:length(uhp)
    for j = 1:length(udb)
        ind   = rv(:,xhp) == uhp(i) & rv(:,xdb) == udb(j);
        hind  = ismember(rv(ind,xrc),h);
        hr(j,i) = sum(hind)/sum(ind);
    end
end
hr(isnan(hr)) = 0;
% compute false-alarm rate
fa = sum(rv(catch_trials,xrc) == f) / sum(catch_trials);

% plot data
cla(ax);

plot(ax,udb,hr','LineWidth',2);

ylim(ax,[0 1]);

hold(ax,'on');

plot(ax,xlim(ax),[fa fa],':r');

hold(ax,'off');

xlabel(ax,'Noise Level (dB)');
ylabel(ax,'%');


legend(ax,num2str(uhp/1000,'%1.0fkHz'),'Location','NorthWest');









